package cn.lcsjsxy.yxc.renerics;

import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {
    @Test
    public void test() {
        //TreeSet<Employee> set = new TreeSet<Employee>();
        //jdk7新特性：类型推断
        TreeSet<Employee> set = new TreeSet();
        set.add(new Employee("A",25,2000,12,20));
        set.add(new Employee("B",22,1990,2,10));
        set.add(new Employee("E",25,2000,12,20));
        set.add(new Employee("P",21,1900,32,22));
        set.add(new Employee("R",99,1000,12,8));

        Iterator<Employee> iterator=set.iterator();
        while (iterator.hasNext()){
            Employee employee=iterator.next();
            System.out.println(employee);
        }
    }

    @Test
    public void test1(){
//        Comparator com=new Comparator() {  方法一
//            @Override
//            public int compare(Object o1, Object o2) {
//                if (o1 instanceof Employee&&o2 instanceof Employee){
//                    Employee e1=(Employee) o1;
//                    Employee e2=(Employee) o2;
//                    int i=Integer.compare(e1.getBirthday().getYear(),e2.getBirthday().getYear());
//                    if (i!=0){
//                        return Integer.compare(e1.getBirthday().getYear(),e2.getBirthday().getYear());
//                    }else {
//                        int n=Integer.compare(e1.getBirthday().getMonth(),e2.getBirthday().getMonth());
//                        if (n!=0){
//
//                            return Integer.compare(e1.getBirthday().getMonth(),e2.getBirthday().getMonth());
//                        }else {
//                            return Integer.compare(e1.getBirthday().getDay(),e2.getBirthday().getDay());
//                        }
//                    }
//                }else
//                    throw new RuntimeException("数据类型错误");
//            }
//        };

        Comparator com=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee&&o2 instanceof Employee){
                    Employee e1=(Employee) o1;
                    Employee e2=(Employee) o2;
                    MyDate myBri1=e1.getBirthday();
                    MyDate myBri2=e2.getBirthday();
                    int minusYear=myBri1.getYear()-myBri2.getYear();
                    if (minusYear!=0)
                        return minusYear;
                    int minusMonth=myBri1.getMonth()-myBri2.getMonth();
                    if (minusMonth!=0)
                        return minusMonth;
                    return myBri1.getDay()-myBri2.getDay();
                }
                throw new RuntimeException("数据类型不一致");
            }
        };

        TreeSet set = new TreeSet(com);
        set.add(new Employee("A",65,2000,12,20));
        set.add(new Employee("B",22,1990,2,10));
        set.add(new Employee("E",25,2000,12,20));
        set.add(new Employee("P",21,1900,12,22));
        set.add(new Employee("R",99,1000,12,8));
        set.add(new Employee("A",45,2000,11,20));
        set.add(new Employee("B",12,1990,2,19));
        set.add(new Employee("E",15,2000,11,20));
        set.add(new Employee("P",61,1900,2,22));
        set.add(new Employee("R",89,1000,11,8));

        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
